<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品" prop="goodsId">
        <el-select v-model="queryParams.goodsId" placeholder="请选择商品" style="width: 100%" clearable>
          <el-option
              v-for="item in goodsOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="平台名称" prop="platformName">
        <el-select v-model="queryParams.platformId" placeholder="请选择平台名称" clearable>
          <el-option
              v-for="item in platformOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:share:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:share:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:share:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:share:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-row>
      <el-col :span="24">
        <span class="all">总金额: {{allAmount}}</span>
        <span class="all">总次数: {{allNum}}</span>
        <span class="all">总重量: {{allWeight}}</span>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="shareList" @selection-change="handleSelectionChange" show-summary>
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="商品名称" align="center" prop="goodsName" width="150px" show-overflow-tooltip/>
      <el-table-column label="平台名称" align="center" prop="platformName" />
      <el-table-column label="分享url" align="center" prop="url" width="200px" show-overflow-tooltip/>
      <el-table-column label="总金额(元)" align="center" prop="amount" width="100px"/>
      <el-table-column label="总次数" align="center" prop="num" />
      <el-table-column label="总重量(KG)" align="center" prop="weight" width="100px"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180px">
        <template slot-scope="scope">
          <span>{{scope.row.createTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180px">
        <template slot-scope="scope">
          <span>{{scope.row.updateTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px" fixed="right">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleCopy(scope.row)"
              v-hasPermi="['acc:platformShare:copy']"
          >复制链接</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:share:edit']"
          >查看</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleOrder(scope.row)"
              v-hasPermi="['acc:platformShare:copy']"
          >查看订单</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:share:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改平台分享对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品id" prop="goodsId">
              <el-select v-model="form.goodsId" placeholder="请选择商品" clearable style="width: 100%">
                <el-option
                    v-for="item in goodsOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="平台名称" prop="platformId">
              <el-select v-model="form.platformId" placeholder="请选择平台" clearable style="width: 100%">
                <el-option
                    v-for="item in platformOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="分享url" prop="url">
              <el-input v-model="form.url" placeholder="自动生成分享url" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="二维码" prop="url">
              <vue-qr
                  :text="form.url"
                  :size="250"
                  :logoScale="0.2">
              </vue-qr>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShare, getShare, delShare, addShare, updateShare, getAll } from "@/api/pets/share";
import { listPlatform } from "@/api/pets/platform";
import { listGoods } from "@/api/pets/goods";
import VueQr from 'vue-qr';
import copyText from "@/utils/copy.js";

export default {
  name: "Share",
  components:{
    VueQr,
  },
  data() {
    return {
      allAmount: null,
      allNum: null,
      allWeight: null,
      goodsOptions: [],
      platformOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 平台分享表格数据
      shareList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: null,
        platformName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsId: [
          { required: true, message: "商品id不能为空", trigger: "blur" }
        ],
        goodsName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        platformId: [
          { required: true, message: "平台id不能为空", trigger: "blur" }
        ],
        platformName: [
          { required: true, message: "平台名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getPlatformOption();
    this.getGoodsOption();
    this.getAllData()
  },
  methods: {
    /** 查询平台分享列表 */
    getList() {
      this.loading = true;
      listShare(this.queryParams).then(response => {
        this.shareList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    getAllData(){
      getAll({
        goodsId: this.queryParams.goodsId
      }).then(response => {
        this.allAmount = response.data.allAmount;
        this.allNum = response.data.allNum;
        this.allWeight = response.data.allWeight;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsId: null,
        goodsName: null,
        platformId: null,
        platformName: null,
        url: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.getAllData()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加平台分享";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getShare(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改平台分享";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShare(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShare(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除平台分享编号为"' + ids + '"的数据项？').then(function() {
        return delShare(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/share/export', {
        ...this.queryParams
      }, `share_${new Date().getTime()}.xlsx`)
    },

    handleCopy(row) {
      copyText(row.url)
      this.$modal.msgSuccess("复制成功");
    },

    handleOrder(row){
      this.$router.push({
        name: 'Orders',
        params: {
          'goodsId': row.goodsId,
          'pfCode': row.pfCode,
        }
      })
    },

    getPlatformOption(){
      listPlatform({pageNum:1, pageSize:1000}).then(response=>{
        this.platformOptions = response.rows.map(platform=>{
          return {
            "value": platform.id,
            "label": platform.name
          }
        })
      })
    },


    getGoodsOption(){
      listGoods().then(response=>{
        this.goodsOptions = response.rows.map(goods=>{
          return {
            "value": goods.id,
            "label": goods.name
          }
        })
      })
    }


  }
};
</script>

<style>
.all {
  margin-right: 50px;
  font-weight: bold;
  font-size: 1rem;
}
</style>
